/*
EP3D is a real-time 3D planet engine , which in addition to providing 
substandard scene rendering and scene management, of course, it also 
provides some basic class libraries to build the entire virtual 
planet, or even the entire universe.

Copyright (C) 2010  Hongjiang Zhang	(zhjwyat@gmail.com)

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/


#ifndef EP3D_MATRIX3_H
#define EP3D_MATRIX3_H
#include "EP3DVector3.h"
#include "EP3DLogger.h"
namespace EP3D
{
	/************************************************************************/
	/*Matrix3 template should only be used with float or double types                                                            
	/************************************************************************/
	template <class T>
	class Matrix3
	{
	public:
		union
		{
			struct  
			{
				T m_11; T m_12; T m_13;
				T m_21; T m_22; T m_23;
				T m_31; T m_32; T m_33;
			};
			T m[3][3];
		};

	public:
		Matrix3(void)
		{
		}

		Matrix3(const T* pt)
			:m_11(pt[0]), m_12(pt[1]), m_13(pt[2])
			,m_21(pt[3]), m_22(pt[4]), m_23(pt[5])
			,m_31(pt[6]), m_32(pt[7]), m_33(pt[8])
		{
		}

		Matrix3(const Matrix3<T>& mat)
			:m_11(mat.m_11), m_12(mat.m_12), m_13(mat.m_13)
			,m_21(mat.m_21), m_22(mat.m_22), m_23(mat.m_23)
			,m_31(mat.m_31), m_32(mat.m_32), m_33(mat.m_33)
		{
		}

		Matrix3(const T _11, const T _12, const T _13,
			const T _21, const T _22, const T _23,
			const T _31, const T _32, const T _33)
			:m_11(_11), m_12(_12), m_13(_13)
			,m_21(_21), m_22(_22), m_23(_23)
			,m_31(_31), m_32(_32), m_33(_33)
		{
		}

		void Set(const T _11, const T _12, const T _13,
			const T _21, const T _22, const T _23,
			const T _31, const T _32, const T _33)
		{
			m_11 = _11; m_12 = _12; m_13 = _13;
			m_21 = _21; m_22 = _22; m_23 = _23;
			m_31 = _31; m_32 = _32; m_33 = _33;
		}

		operator T* ()
		{
			return (T*)&m_11;
		}

		operator const T* () const
		{
			return (const T*)&m_11;
		}

		T& operator () (uint uiRow, uint uiCol)
		{
			return m[uiRow][uiCol];
		}

		T operator () (uint uiRow, uint uiCol) const
		{
			return m[uiRow][uiCol];
		}

		T* operator [] (uint uiRow) const
		{
			return (T*)m[uiRow];
		}

		Matrix3<T>&  operator += (const Matrix3<T>& mat)
		{
			m_11 += mat.m_11; m_12 += mat.m_12; m_13 += mat.m13;
			m_21 += mat.m_21; m_22 += mat.m_22; m_23 += mat.m_23;
			m_31 += mat.m_31; m_32 += mat.m_32; m_33 += mat.m_33;

			return *this;
		}

		Matrix3<T>&  operator -= (const Matrix3<T>& mat)
		{
			m_11 -= mat.m_11; m_12 -= mat.m_12; m_13 -= mat.m13;
			m_21 -= mat.m_21; m_22 -= mat.m_22; m_23 -= mat.m_23;
			m_31 -= mat.m_31; m_32 -= mat.m_32; m_33 -= mat.m_33;

			return *this;
		}

		Matrix3<T>&  operator *= (const Matrix3<T>& mat)
		{
			m_11 = m_11 * mat.m_11 + m_12 * mat.m_21 + m_13 * mat.m_31;
			m_12 = m_11 * mat.m_12 + m_12 * mat.m_22 + m_13 * mat.m_32;
			m_13 = m_11 * mat.m_13 + m_12 * mat.m_23 + m_13 * mat.m_33;

			m_21 = m_21 * mat.m_11 + m_22 * mat.m_21 + m_23 * mat.m_31;
			m_22 = m_21 * mat.m_12 + m_22 * mat.m_22 + m_23 * mat.m_32;
			m_23 = m_21 * mat.m_13 + m_22 * mat.m_23 + m_23 * mat.m_33;

			m_31 = m_31 * mat.m_11 + m_32 * mat.m_21 + m_33 * mat.m_31;
			m_32 = m_31 * mat.m_12 + m_32 * mat.m_22 + m_33 * mat.m_32;
			m_33 = m_31 * mat.m_13 + m_32 * mat.m_23 + m_33 * mat.m_33;

			return *this;
		}

		Matrix3<T>& operator *= (const T t)
		{
			m_11 *= t; m_12 *= t; m_13 *= t;
			m_21 *= t; m_22 *= t; m_23 *= t;
			m_31 *= t; m_32 *= t; m_33 *= t;
			return *this;
		}

		Matrix3<T>& operator /= (const T t)
		{
			T tInv = 1.0f/t;
			m_11 *= tInv; m_12 *= tInv; m_13 *= tInv;
			m_21 *= tInv; m_22 *= tInv; m_23 *= tInv;
			m_31 *= tInv; m_32 *= tInv; m_33 *= tInv;
			return *this;
		}

		Matrix3<T> operator + (void) const
		{
			return *this;
		}

		Matrix3<T> operator - (void) const
		{
			return Matrix3<T>(
				-m_11, -m_12, -m_13,
				-m_21, -m_22, -m_23,
				-m_31, -m_32, -m_33);
		}

		Matrix3<T> operator + (const Matrix3<T>& mat) const
		{
			return Matrix3<T>(
				m_11 + mat.m_11, m_12 + mat.m_12, m_13 + mat.m_13,
				m_21 + mat.m_21, m_22 + mat.m_22, m_23  + mat.m_23,
				m_31 + mat.m_31, m_32 + mat.m_32, m_33 + mat.m_33);
		}

		Matrix3<T> operator - (const Matrix3<T>& mat) const
		{
			return Matrix3<T>(
				m_11 - mat.m_11, m_12 - mat.m_12, m_13 - mat.m_13,
				m_21 - mat.m_21, m_22 - mat.m_22, m_23  - mat.m_23,
				m_31 - mat.m_31, m_32 - mat.m_32, m_33 - mat.m_33);
		}

		Matrix3<T> operator * (const Matrix3<T>& mat) const
		{
			Matrix3<T> matT;

			matT.m_11 = m_11 * mat.m_11 + m_12 * mat.m_21 + m_13 * mat.m_31;
			matT.m_12 = m_11 * mat.m_12 + m_12 * mat.m_22 + m_13 * mat.m_32;
			matT.m_13 = m_11 * mat.m_13 + m_12 * mat.m_23 + m_13 * mat.m_33;

			matT.m_21 = m_21 * mat.m_11 + m_22 * mat.m_21 + m_23 * mat.m_31;
			matT.m_22 = m_21 * mat.m_12 + m_22 * mat.m_22 + m_23 * mat.m_32;
			matT.m_23 = m_21 * mat.m_13 + m_22 * mat.m_23 + m_23 * mat.m_33;

			matT.m_31 = m_31 * mat.m_11 + m_32 * mat.m_21 + m_33 * mat.m_31;
			matT.m_32 = m_31 * mat.m_12 + m_32 * mat.m_22 + m_33 * mat.m_32;
			matT.m_33 = m_31 * mat.m_13 + m_32 * mat.m_23 + m_33 * mat.m_33;

			return matT;
		}

		Vector3<T> operator * (const Vector3<T>& v) const
		{
			return Vector3<T>(
				m_11 * v.x + m_12 * v.y + m_13 * v.z,
				m_21 * v.x + m_22 * v.y + m_23 * v.z,
				m_31 * v.x + m_32 * v.y + m_33 * v.z);
		}

		friend Vector3<T> operator * (const Vector3<T>& v, const Matrix3<T>& mat)
		{
			return Vector3<T>(
				m_11 * v.x + m_21 * v.y + m_31 * v.z,
				m_12 * v.x + m_22 * v.y + m_32 * v.z,
				m_13 * v.x + m_23 * v.y + m_33 * v.z);
		}

		Matrix3<T> operator * (const T t) const
		{
			return Matrix3<T>(
				m_11 * t, m_12 * t, m_13 * t,
				m_21 * t, m_22 * t, m_23 * t,
				m_31 * t, m_32 * t, m_33 * t);
		}

		friend Matrix3<T> operator* (const T t, const Matrix3<T>& mat)
		{
			return Matrix3<T>(
				t * mat.m_11, t * mat.m_12, t * mat.m_13,
				t * mat.m_21, t * mat.m_22, t * mat.m_23,
				t * mat.m_31, t * mat.m_32, t * mat.m_33);
		}

		Matrix3<T> operator / (const T t) const
		{
			T tInv = 1.0f/t;
			return Matrix3<T>(
				m_11 * tInv, m_12 * tInv, m_13 * tInv,
				m_21 * tInv, m_22 * tInv, m_23 * tInv,
				m_31 * tInv, m_32 * tInv, m_33 * tInv);
		}

		bool operator == (const Matrix3<T>& mat) const
		{
			return 0 == memcmp(&m_11, &mat, sizeof(Matrix3<T>));
		}

		bool operator != (const Matrix3<T>& mat) const
		{
			return 0 != memcmp(&m_11, &mat, sizeof(Matrix3<T>));
		}

		Vector3<T> GetColumn(uint uiCol) const
		{
			LogAssert(uiCol >= 0 && uiCol < 3);
			return Vector3<T>(m[0][uiCol], m[1][uiCol], m[2][uiCol]);
		}

		void SetColumn(uint uiCol, const Vector3<T>& v)
		{
			LogAssert(uiCol >= 0 && uiCol < 3);
			m[0][uiCol] = v.x;
			m[1][uiCol] = v.y;
			m[2][uiCol] = v.z;
		}

		void FromAxis(const Vector3<T>& xAxis, const Vector3<T>& yAxis, const Vector3<T>& zAxis)
		{
			SetColumn(0, xAxis);
			SetColumn(1, yAxis);
			SetColumn(2, zAxis);
		}

		void FromAxisAngle(const T tAngle, const Vector3<T>& n)
		{
			T tCos = Math<T>::Cos(tAngle);
			T tSin = Math<T>::Sin(tAngle);
			T tOneMinusCos = 1.0f-tCos;
			T tX2 = n.x*n.x;
			T tY2 = n.y*n.y;
			T tZ2 = n.z*n.z;
			T tXYM = n.x*n.y*tOneMinusCos;
			T tXZM = n.x*n.z*tOneMinusCos;
			T tYZM = n.y*n.z*tOneMinusCos;
			T tXSin = n.x*tSin;
			T tYSin = n.y*tSin;
			T tZSin = n.z*tSin;

			m[0][0] = tX2*tOneMinusCos+tCos;
			m[0][1] = tXYM-tZSin;
			m[0][2] = tXZM+tYSin;
			m[1][0] = tXYM+tZSin;
			m[1][1] = tY2*tOneMinusCos+tCos;
			m[1][2] = tYZM-tXSin;
			m[2][0] = tXZM-tYSin;
			m[2][1] = tYZM+tXSin;
			m[2][2] = tZ2*tOneMinusCos+tCos;
		}

		T Determinant() const
		{
			T tCofactor00 = m[1][1]*m[2][2] -m[1][2]*m[2][1];
			T tCofactor10 = m[1][2]*m[2][0] -m[1][0]*m[2][2];
			T tCofactor20 = m[1][0]*m[2][1] -m[1][1]*m[2][0];

			T tDet =m[0][0]*tCofactor00 +m[0][1]*tCofactor10 +m[0][2]*tCofactor20;

			return tDet;
		}

		Matrix3<T> Transpose() const
		{
			Matrix3<T> tTranspose;
			for(uint uiRow = 0; uiRow < 3; ++uiRow)
			{
				for(uint uiCol = 0; uiCol < 3; ++ uiCol)
					tTranspose[uiRow][uiCol] = m[uiCol][uiRow];
			}
			return tTranspose;
		}

		void Zero()
		{
			*this = 0.0f;
		}

		void Identify()
		{
			m[0][0] = 1.0f; m[0][1] = 0.0f; m[0][2] = 0.0f;
			m[1][0] = 0.0f; m[1][1] = 1.0f; m[1][2] = 0.0f;
			m[2][0] = 0.0f; m[2][1] = 0.0f; m[2][2] = 1.0f;
		}

	};

	typedef Matrix3<f32> Matrix3f;
	typedef Matrix3<f64> Matrix3d;
}

#endif